package com.firefly.service.command.ums;

import com.firefly.dao.dao.ums.po.MenuPo;
import com.firefly.dao.assembler.ums.MenuAssembler;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.dto.ums.MenuDto;
import com.firefly.dao.dto.ums.MenuSortDto;
import com.firefly.service.repository.ums.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 权限菜单领域服务
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/11/29
 **/
@Service
public class MenuCommand {
    private final MenuRepository menuRepository;
    private final MenuAssembler menuAssembler;

    @Autowired
    public MenuCommand(MenuRepository menuRepository, MenuAssembler menuAssembler){
        this.menuRepository = menuRepository;
        this.menuAssembler = menuAssembler;
    }
    public boolean create(MenuDto request){
        MenuPo entity = menuAssembler.getPo(request);
        return menuRepository.createMenu(entity);
    }
    public boolean update(MenuDto request){
        if (request.getId() == request.getParentId()){
            throw new FireflyException(403,"父级不能选择自己");
		}
        MenuPo entity = menuAssembler.getPo(request);
        return menuRepository.updateByid(entity);
    }
    public boolean sort(MenuSortDto request){
        MenuPo entity = menuAssembler.getPo(request);
        return menuRepository.sortById(entity);
    }
    public boolean removeById(Integer id){
        List<Integer> ids = menuRepository.getIds(id);
        return menuRepository.removeId(ids);
    }
}
